import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;


public class AddEditMovieDialog extends JDialog {
	private static final long serialVersionUID = 5628061462335602671L;
	private final JPanel contentPanel = new JPanel();
	private JButton confirmButton;
	public String title;
	public int time;
	private String[] titles;
	private AdminEditShowingsPanel panel;
	private JTextField timeBox;
	private JComboBox<String> movieBox;

	/**
	 * Create the dialog.
	 */
	public AddEditMovieDialog(DatabaseInfo the_db_info, String edit_or_add) {
		setBounds(100, 100, 340, 219);
		setVisible(false);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			JLabel lblAddMovie;
			if (edit_or_add.equals("add"))
				lblAddMovie = new JLabel("Add Movie");
			else 
				lblAddMovie = new JLabel("Edit Movie");

			lblAddMovie.setFont(new Font("Tahoma", Font.BOLD, 16));
			lblAddMovie.setBounds(113, 11, 98, 14);
			contentPanel.add(lblAddMovie);
		}
		{
			titles = popluateComboBox(the_db_info);
			movieBox = new JComboBox<String>(titles);
			title = titles[0];
			movieBox.setName("title");
			movieBox.addActionListener(new ComboListener());
			movieBox.setBounds(83, 50, 206, 20);
			contentPanel.add(movieBox);
		}
		{
			timeBox = new JTextField();

			timeBox.setBounds(83, 94, 103, 20);
			contentPanel.add(timeBox);
		}
		{
			JLabel lblSelectAMovie = new JLabel("Select a Movie:");
			lblSelectAMovie.setFont(new Font("Segoe UI", Font.PLAIN, 9));
			lblSelectAMovie.setBounds(83, 36, 101, 14);
			contentPanel.add(lblSelectAMovie);
		}
		{
			JLabel lblChooseATime = new JLabel("Choose a start time:");
			lblChooseATime.setFont(new Font("Segoe UI", Font.PLAIN, 9));
			lblChooseATime.setBounds(83, 81, 130, 14);
			contentPanel.add(lblChooseATime);
		}
		{
			JLabel lblMovie = new JLabel("Movie:");
			lblMovie.setFont(new Font("Tahoma", Font.PLAIN, 14));
			lblMovie.setBounds(34, 51, 46, 14);
			contentPanel.add(lblMovie);
		}
		{
			JLabel lblTime = new JLabel("Time:");
			lblTime.setFont(new Font("Tahoma", Font.PLAIN, 14));
			lblTime.setBounds(34, 95, 46, 14);
			contentPanel.add(lblTime);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				confirmButton = new JButton("Confirm");
				confirmButton.setActionCommand("Confirm");
				confirmButton.addActionListener(new ConfirmListener());
				buttonPane.add(confirmButton);
				getRootPane().setDefaultButton(confirmButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent arg0) {
						setVisible(false);
					}
				});
				buttonPane.add(cancelButton);
			}
		}

		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
	}

	private String[] popluateComboBox(DatabaseInfo dbi) {
		Db db = Db.getDb();
		String[] tuple = null;
		String query = null;
		query = "SELECT movies.title FROM movies";

		String result = null;

		try {
			result = db.executeQuery2(dbi.server, dbi.database, query, 
					dbi.user_name, dbi.password);
		} catch (Exception e1) {
			System.out.println(e1.getMessage());
			e1.printStackTrace();
		}

		if(result != null) {
			tuple = result.split("@");
		}
		return tuple;
	}

	public void setAdminEditPanel(AdminEditShowingsPanel p) {
		panel = p;
	}
	
	public void setCurrentMovieTime(String movie, int time) {
		movieBox.setSelectedItem(movie);
		timeBox.setText(String.valueOf(time));
	}

	private class ComboListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			@SuppressWarnings("unchecked")
			JComboBox<String> drop_down = ((JComboBox<String>) e.getSource());
			if (drop_down.getSelectedIndex() > -1) {
				title = titles[drop_down.getSelectedIndex()];
			}

		}
	}

	private class ConfirmListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			int temp = 0;
			if (timeBox.getText() != null) {
				try {
					temp = Integer.parseInt(timeBox.getText());
					if (temp > 0 && temp < 2400) {
						int lastTwo = temp % 100;
						int firstTwo = temp / 100;
						if (lastTwo < 60 && firstTwo < 24) {
							time = temp;
							panel.modifyTable();
							setVisible(false);
						} else {
							JOptionPane.showMessageDialog(null,
									"Please enter a correct time with the first two digits from 0-12 and the last two from 0-59.",
									"Error",
									JOptionPane.ERROR_MESSAGE);
						}
					} else {
						JOptionPane.showMessageDialog(null,
								"Please enter a time from 0 to 2399.",
								"Error",
								JOptionPane.ERROR_MESSAGE);
					}
				} catch (Exception e1) {
					JOptionPane.showMessageDialog(null,
							"Please enter a number.",
							"Error",
							JOptionPane.ERROR_MESSAGE);
				}
			} else {
				JOptionPane.showMessageDialog(null,
						"Please enter a time.",
						"Error",
						JOptionPane.ERROR_MESSAGE);
			}
		}
	}

}
